package com.example.controller;

import com.example.domain.Answer;
import com.example.domain.Option;
import com.example.domain.Question;
import com.example.domain.Questionnaire;
import com.example.mapper.OptionMapper;
import com.example.service.AnswerService;
import com.example.service.OptionService;
import com.example.service.QuestionService;
import com.example.service.QuestionnaireService;
import com.example.service.impl.QuestionnaireServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DPH
 * @date 2022/12/12
 */
@Controller
@RequestMapping("/questionnaire")
public class QuestionnaireController {

    @Autowired
    QuestionnaireService questionnaireService;
    @Autowired
    OptionService optionService;

    @Autowired
    AnswerService answerService;

    @Autowired
    QuestionService questionService;


    @GetMapping("/{id}")
    public String questionnaireFill(@PathVariable("id") String id, HttpSession httpSession) {
        Questionnaire questionnaire = questionnaireService.findById(Integer.parseInt(id));
        System.out.println(questionnaire.getStatus());
        httpSession.setAttribute("questionnaire", questionnaire);
       // Questionnaire questionnaire = questionnaireService.findById(Integer.parseInt(id));
        List<Question> questionList =questionnaireService.getQuestions(Integer.parseInt(id));
        System.out.println(questionList);
        httpSession.setAttribute("questionList",questionList );
        List<Option> optionList = optionService.getOption2(Integer.parseInt(id));
        httpSession.setAttribute("optionList",optionList );
        System.out.println(optionList);
        return "UserFillIn";
    }

    @GetMapping("/{id}/show")
    public String questionnaireResultShow(@PathVariable("id") String id, HttpSession httpSession) {
        Questionnaire questionnaire = questionnaireService.findById(Integer.parseInt(id));
        httpSession.setAttribute("questionnaire", questionnaire);
        // Questionnaire questionnaire = questionnaireService.findById(Integer.parseInt(id));
        List<Question> questionList =questionnaireService.getQuestions(Integer.parseInt(id));
        httpSession.setAttribute("questionList",questionList );
        List<Option> optionList = optionService.getOption(Integer.parseInt(id));
        httpSession.setAttribute("optionList",optionList);
        Map<Integer,Map<String,Integer>> map = answerService.getAnswer(questionService.getMCQ(Integer.parseInt(id)));
        System.out.println(map);
        List<Question> answerList = questionService.getCloze(Integer.parseInt(id));
        Map<Integer,List<String>> map1 = answerService.getClozeAnswer(answerList);
        httpSession.setAttribute("map",map1);
        System.out.println(map1);
//        Map<String,Integer> answerMap = answerService.getAnswer(Integer.valueOf(id));
//        Map<Integer,Map<String,Integer>>
//        httpSession.setAttribute("answerList",answerList );
//        Map<String,Integer> answerMap = new HashMap<>();
//        answerMap.put("Csdrwsd",15);
        httpSession.setAttribute("answerMap",map );
        return "QuestionnaireResultShow";
    }


//    @GetMapping("/submitquestionnaire1")
//    public String questionnaireFill2(@RequestParam("radio1") String SChioce,@RequestParam("radio2") String MChioce) {
//        System.out.println(SChioce);
//        System.out.println(MChioce);
//        return "UserFillIn";
//    }

    @GetMapping("/submitquestionnaire")
    public String questionnaireFill2(HttpServletRequest request,HttpSession httpSession) {
        int questionnaireId = Integer.parseInt(request.getParameter("questionnaireSChoice"));
        System.out.println(questionnaireService.getSChioceNum(questionnaireId));
        List<Question> SChioceList = questionnaireService.getSChoiceQuestions(questionnaireId);
        List<Question> BlankList = questionnaireService.getBlankQuestions(questionnaireId);
        int BlankId = BlankList.get(0).getQuestionId();
        System.out.println(SChioceList);
        String MChoice[] =request.getParameterValues("radioM");
            for (Question SChioce : SChioceList) {
                String SChoice = request.getParameter("radio" + SChioce.getContent());
                System.out.println("radio" + SChioce.getContent());
                System.out.println(SChoice);
                if (SChoice != null) {
                    String[] str = SChoice.split("/");
                    int questionId = Integer.parseInt(str[0]);
                    String option = str[1];
                    Answer answer = new Answer();
                    answer.setQuestionId(questionId);
                    answer.setContent(option);
                    answerService.saveAnswer(answer);
                }
            }
            for (Question Blank : BlankList) {
                String BlankContent = request.getParameter("radio" + Blank.getQuestionNum());
                System.out.println("radio" + Blank.getContent());
                System.out.println(BlankId);
                if (!BlankContent.equals("")) {
                    int questionId = BlankId;
                    String option = BlankContent;
                    Answer answer = new Answer();
                    answer.setQuestionId(questionId);
                    answer.setContent(option);
                    answerService.saveAnswer(answer);
                }
                BlankId++;
            }
            if (MChoice != null) {
                for (int i = 0; i < MChoice.length; i++) {
                    String[] str = MChoice[i].split("/");
                    int questionId = Integer.parseInt(str[0]);
                    String option = str[1];
                    Answer answer = new Answer();
                    answer.setQuestionId(questionId);
                    answer.setContent(option);
                    answerService.saveAnswer(answer);
                }
            }
        return "SuccessPage";
    }
//    @GetMapping("/submitquestionnaire")
//    public String questionnaireFill2(HttpServletRequest request,HttpSession httpSession) {
//        int questionnaireId = Integer.parseInt(request.getParameter("questionnaireSChoice"));
//        System.out.println(questionnaireService.getSChioceNum(questionnaireId));
//        int SChioceNum = questionnaireService.getSChioceNum(questionnaireId);
//        String MChoice[] =request.getParameterValues("radioM");
//        String SChoice1 = request.getParameter("radio1");
//        String SChoice2 = request.getParameter("radio4");
//        System.out.println(SChoice1);
//        System.out.println(SChoice2);
//        List<String> SChoice = new ArrayList<>();
//        if (SChioceNum ==1){
//            SChoice.add(request.getParameter("radio1"));
//        }
//        if (SChioceNum ==2){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//        }
//        if (SChioceNum ==3){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//        }
//        if (SChioceNum ==4){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//        }
//        if (SChioceNum ==5){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//            SChoice.add(request.getParameter("radio5"));
//        }
//        if (SChioceNum ==6){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//            SChoice.add(request.getParameter("radio5"));
//            SChoice.add(request.getParameter("radio6"));
//        }
//        if (SChioceNum ==7){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//            SChoice.add(request.getParameter("radio5"));
//            SChoice.add(request.getParameter("radio6"));
//            SChoice.add(request.getParameter("radio7"));
//        }
//        if (SChioceNum ==8){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//            SChoice.add(request.getParameter("radio5"));
//            SChoice.add(request.getParameter("radio6"));
//            SChoice.add(request.getParameter("radio7"));
//            SChoice.add(request.getParameter("radio8"));
//        }
//        if (SChioceNum ==9){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//            SChoice.add(request.getParameter("radio5"));
//            SChoice.add(request.getParameter("radio6"));
//            SChoice.add(request.getParameter("radio7"));
//            SChoice.add(request.getParameter("radio8"));
//            SChoice.add(request.getParameter("radio9"));
//        }
//        if (SChioceNum ==10){
//            SChoice.add(request.getParameter("radio1"));
//            SChoice.add(request.getParameter("radio2"));
//            SChoice.add(request.getParameter("radio3"));
//            SChoice.add(request.getParameter("radio4"));
//            SChoice.add(request.getParameter("radio5"));
//            SChoice.add(request.getParameter("radio6"));
//            SChoice.add(request.getParameter("radio7"));
//            SChoice.add(request.getParameter("radio8"));
//            SChoice.add(request.getParameter("radio9"));
//            SChoice.add(request.getParameter("radio10"));
//        }
////        String SChoice = request.getParameter("radio1");
////        String SChoice2 = request.getParameter("radio4");
//        String BlankContent = request.getParameter("radio3");
////        int BlankId = Integer.parseInt(request.getParameter("radio4"));
//            if (SChoice.size() != 0){
//                for (int j = 0; j < SChioceNum; j++) {
//                    String[] str =  SChoice.get(j).split ("/");
//                    int questionId = Integer.parseInt(str[0]);
//                    String option = str[1];
//                    Answer answer = new Answer();
//                    answer.setQuestionId(questionId);
//                    answer.setContent(option);
//                    answerService.saveAnswer(answer);
//                }}
//
//        if (MChoice!=null && SChoice != null && !BlankContent.equals("")){
//            if (MChoice != null) {
//                for (int i = 0; i < MChoice.length; i++) {
//                    String[] str =  MChoice[i].split ("/");
//                    int questionId = Integer.parseInt(str[0]);
//                    String option = str[1];
//                    Answer answer = new Answer();
//                    answer.setQuestionId(questionId);
//                    answer.setContent(option);
//                    answerService.saveAnswer(answer);
//                }
//            }
////            if (SChoice != null) {
////                String[] str =  SChoice.split ("/");
////                int questionId = Integer.parseInt(str[0]);
////                String option = str[1];
////                Answer answer = new Answer();
////                answer.setQuestionId(questionId);
////                answer.setContent(option);
////                answerService.saveAnswer(answer);
////            }
////            if (!BlankContent.equals("")) {
////                int questionId = BlankId;
////                String option = BlankContent;
////                Answer answer = new Answer();
////                answer.setQuestionId(questionId);
////                answer.setContent(option);
////                answerService.saveAnswer(answer);
////            }
//            System.out.println(SChoice);
//            return "SuccessPage";
//        } else {
//            request.setAttribute("message", "提交失败：请做完全部题目");
//            return "UserFillIn";
//        }
//    }

}